<?php

namespace Admin\Controller;
use Think\Controller;
class WithdrawalsController  extends IndexController {
    public function lists(){

        $this->display();
    }
    public function get_money_log_list(){
        $ManagerMoneyLog=D("ManagerMoneyLog"); 
        $Doctor=D("Doctor");

        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        if(I("request.sSearch")!=''){
            $where['name'] = array('like','%'.I("request.sSearch").'%');;
        }else{
            $where='';
        }



        $res=$ManagerMoneyLog->order("id desc")
            ->limit($start,I("request.iDisplayLength"))
            ->where($where)
            ->select();
        if(!$res){
            echo json_encode(0); exit;
        }
        $shop=D("ShopBase");
        $Manager=D("Manager");
        foreach($res as &$v){
            $r=$Manager->where("id=".$v['manager_id'])->find();
            $v['manager_name']=$r['username'];
            $rr=$shop->where("id=".$r['shop_id'])->find();
            $v['shop_name']=$rr['name'];
            if($v['doctor_id']!=null){
                $rrr=$Doctor->where("id=".$v['doctor_id'])->find();
                $v['doctor_id']=$rrr['name'];
            }else{
                $v['doctor_id']="无";
            }
            $v['bz']=$rr['bz'];
            $v['to_money']=$v['money']-$v['s_money'];
            $v['create_time']=  date("Y-m-d H:i:s",$v['create_time']);
         
            if($v['update_time']!=null){
                $v['update_time']=  date("Y-m-d H:i:s",$v['update_time']);
            }
            unset($v['log']);
            unset($v['manager_id']);
        }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" => $ManagerMoneyLog->count(),
            "aaData" => $res
        );

        echo json_encode($ret);exit;
    }







    public function confirm($id){
        if($_SESSION['name']!='admin'){
            echo json_encode(0); exit;
        }
        $ManagerMoneyLog=D("ManagerMoneyLog"); 
        $res=$ManagerMoneyLog->where("id=".$id)->find();
        if(!$res){
            echo json_encode(0); exit;
        }
        $data['commit_user']=$_SESSION['name'];
        $data['update_time']=time();
        $data['action']=2;
        $data['log']=$res['log'].",已到达账户,请注意核对";
        $r=$ManagerMoneyLog->where("id=".$id)->save($data);
        
        if(!$r){
            echo json_encode(0); exit;
        }
        $Doctor=D("Doctor"); 
        if($res['doctor_id']!=null){
                $rr=$Doctor->where('id='.$res['doctor_id'])->setDec("cash",  $res['money']-$res['s_money']);
                if(!$rr){
                    echo json_encode(0); exit;
                }
        }else{
            //减掉商户提现金额
            $manager=D("Manager");
            $rr=$manager->where('id='.$res['manager_id'])->setDec("cash",$res['money']-$res['s_money']);
            if(!$rr){
                echo json_encode(0); exit;
            }
        }
        echo json_encode(1); exit;
    }

}